
import java.io.IOException;
import java.util.Vector;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.game.TiledLayer;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author LONGBK
 */
public class WCMap {
    private Image wcIMG;
    private TiledLayer map;
    private TiledLayer wall;
    private int width, height;
    private int viewPortX, viewPortY;
    private int savePortX, savePortY;
    private int[][] tiles;
    private int mrLY, mrLX;
    private Image namIconIMG;
    private Image nuIconIMG;
    private Image cayIMG;
    private Sprite cay;
    private Vector listCay;
    private Sprite namIcon, nuIcon;
    private NPC npc;
    private Image npcIMG;
    private Image ncIMG;
    private Sprite nc;
    private Image smokeIMG;
    private Sprite smoke;
    public WCMap(){
        
    }
    public void loadMap(SunnetGameCanvas gameCanvas){
        width = gameCanvas.getWidth();
        height = gameCanvas.getHeight();
        viewPortX = 0;
        viewPortY = 90;
        listCay = new Vector(3);
        tiles = new int[][]{
                { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                { 1, 2, 3, 4, 5, 2, 2, 6, 2, 2, 3, 4, 5, 2, 2 },
                { 8, 18, 9, 10, 9, 10, 9, 13, 10, 18, 10, 9, 10, 9, 10 },
                { 8, 16, 16, 16, 16, 17, 16, 20, 16, 16, 17, 12, 12, 12, 16 },
                { 22, 11, 11, 11, 11, 11, 27, 24, 22, 11, 11, 11, 11, 11, 27 },
                { 22, 11, 11, 11, 11, 11, 19, 24, 22, 11, 11, 11, 11, 11, 19 },
                { 22, 11, 11, 11, 11, 11, 26, 24, 22, 11, 11, 11, 11, 11, 26 },
                { 22, 11, 11, 11, 11, 11, 27, 24, 22, 11, 11, 11, 11, 11, 27 },
                { 22, 11, 11, 11, 11, 11, 19, 24, 22, 11, 11, 11, 11, 11, 19 },
                { 22, 11, 11, 11, 11, 11, 26, 24, 22, 11, 11, 11, 11, 11, 26 },
                { 22, 11, 11, 11, 11, 11, 27, 24, 22, 11, 11, 11, 11, 11, 27 },
                { 22, 11, 11, 11, 11, 11, 19, 24, 22, 11, 11, 11, 11, 11, 19 },
                { 22, 11, 11, 11, 11, 11, 26, 24, 22, 11, 11, 11, 11, 11, 26 },
                { 22, 11, 11, 11, 11, 11, 27, 24, 22, 11, 11, 11, 11, 11, 27 },
                { 2, 2, 11, 11, 2, 2, 2, 2, 2, 2, 2, 11, 11, 2, 2 },
                { 10, 10, 11, 11, 10, 10, 10, 10, 10, 10, 10, 11, 11, 10, 10 },
                { 16, 16, 11, 11, 16, 16, 16, 16, 16, 16, 16, 11, 11, 16, 16 },
                { 14, 7, 14, 14, 14, 14, 7, 14, 14, 14, 14, 14, 14, 14, 7 },
                { 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14 },
                { 14, 14, 14, 7, 14, 14, 14, 14, 14, 14, 7, 14, 14, 14, 14 },
                { 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14 }
            };
        try {
            wcIMG = Image.createImage("/wc.png");
            namIconIMG = Image.createImage("/nam.png");
            nuIconIMG = Image.createImage("/nu.png");
            cayIMG = Image.createImage("/caycanh.png");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        map = new TiledLayer(15, 23, wcIMG, 16, 16);
        for (int row = 0; row < 23; row++) {
            for (int col = 0; col < 15; col++) {
                map.setCell(col, row, tiles[row][col]);
            }
        }
        wall = new TiledLayer(15, 3, wcIMG, 16, 16);         
        for (int row = 0; row < 3; row++) {
            for (int col = 0; col < 15; col++) {
                if(col != 2 && col != 3 && col != 11 && col != 12)
                    wall.setCell(col, row, tiles[row + 16][col]);
                else
                    wall.setCell(col, row, 0);
            }
        }      
        namIcon = new Sprite(namIconIMG, 22, 28);
        nuIcon = new Sprite(nuIconIMG, 22, 28);
        wall.setPosition(0, 16 * 16);
        namIcon.setPosition(9 * 16 + 5, 16 * 16 + 8);
        nuIcon.setPosition(4 * 16 + 5, 16 * 16 + 8);
        gameCanvas.getMrL().setPosition(8 * 16, 20 * 16);
        map.setPosition(0, 0);
        gameCanvas.getLayerManager().append(gameCanvas.getMrL());
        gameCanvas.getLayerManager().append(namIcon);
        gameCanvas.getLayerManager().append(nuIcon);
        for(int index = 0; index < 3; index ++){
            cay = new Sprite(cayIMG, 16, 41);
            if(index == 0)
                cay.setPosition(7 * 16, 19 * 16 - 41 + 16);
            if(index == 1)
                cay.setPosition(0, 6 * 16 - 41 + 16);
            if(index == 2)
                cay.setPosition(8 * 16, 6 * 16 - 41 + 16);
            listCay.addElement(cay);
            gameCanvas.getLayerManager().append(cay);
        }
        /*
        for(int index = 2; index < 6; index ++){
            npc = NPC.elementAt(index);
            if(npc.type == 2){
                if(index == 2){
                    npc.setPosition(8 * 16, 7 * 16);
                    npc.startC = 8;
                    npc.startR = 7;
                }
                if(index == 3){
                    npc.setPosition(12 * 16, 13 * 16);
                    npc.startC = 12;
                    npc.startR = 13;
                }
            }
            if(npc.type == 3){
                npc.setPosition(8 * 16, 10 * 16);
                npc.startC = 8;
                npc.startR = 10;
                npc.setDefaultStage(3);
                npc.setFrameSequence(npc.stopSeq);
            }
            if(npc.type == 4){
                npc.setPosition(11 * 16, 9 * 16);
                npc.startC = 11;
                npc.startR = 9;
                npc.setDefaultStage(4);
            }
        }
        
         *
         */
        for(int index = 0; index < 6; index ++){
            if(index == 0){
                if(npcIMG == null){
                        try {
                            npcIMG = Image.createImage("/cop.png");
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                    }
                npc = new NPC(npcIMG, 16, 32, 2);
            }
            if(index == 1){
                npc = new NPC(npcIMG, 16, 32, 2);
                npcIMG = null;
            }
            if (index == 2) {
                //if(NPC.listNPC.isEmpty()){
                    if(npcIMG == null){
                        try {
                            npcIMG = Image.createImage("/npc3.png");
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                    }
                    npc = new NPC(npcIMG, 16, 32, 2);
                //}

                npc.setPosition(8 * 16, 7 * 16);
                    npc.startC = 8;
                    npc.startR = 7;
            }
            if (index == 3) {
                npc = new NPC(npcIMG, 16, 32, 2);
                npc.setPosition(12 * 16, 13 * 16);
                    npc.startC = 12;
                    npc.startR = 13;
                npcIMG = null;
            }
            if (index == 4) {
                //if(NPC.listNPC.isEmpty()){
                    if(npcIMG == null){
                        try {
                            npcIMG = Image.createImage("/npc1.png");
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                    }
                    npc = new NPC(npcIMG, 16, 32, 3);
               // }

                 npc.setPosition(8 * 16, 10 * 16);
                npc.startC = 8;
                npc.startR = 10;
                //npc.setDefaultStage(3);
                npc.setFrameSequence(npc.stopSeq);
                npcIMG = null;
            }
            if (index == 5) {
                //if(NPC.listNPC.isEmpty()){
                    if(npcIMG == null){
                        try {
                            npcIMG = Image.createImage("/npc2.png");
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                    }
                    npc = new NPC(npcIMG, 16, 32, 4);
                //}

                 npc.setPosition(11 * 16, 9 * 16);
                npc.startC = 11;
                npc.startR = 9;
                //npc.setDefaultStage(4);
                npc.setFrameSequence(new int[]{12});
                npcIMG = null;
            }
            if(index != 0 && index != 1)
                gameCanvas.getLayerManager().append(npc);
            gameCanvas.getLayerManager().append(map);
        }     
    }
    public boolean getColides(SunnetGameCanvas gameCanvas){
        boolean _return = false;
        mrLX = gameCanvas.getMrL().getX();
        mrLY = gameCanvas.getMrL().getY();
        if(mrLY == 4 * 16 + 8){
            gameCanvas.key2 = false;
            gameCanvas.press2 = false;
            _return = true;
        }
        if(mrLY == 21 * 16 || mrLY == 17 * 16  && ((mrLX < 2 * 16) || (mrLX >= 4 * 16 - 8 && mrLX < 11 * 16) || (mrLX >= 13 * 16 - 8))){
            gameCanvas.key8 = false;
            gameCanvas.press8 = false;
            _return = true;
        }
        if(mrLX == 0 || (mrLX == 7 * 16 + 8 && mrLY < 17 * 16 + 8)){
            gameCanvas.key4 = false;
            gameCanvas.press4 = false;
            _return = true;
        }
        if((mrLX == 14 * 16 && mrLY > 17 * 16 + 8) || (mrLX == 13 * 16 && mrLY < 17 * 16 + 8) || (mrLY < 17 * 16 + 8 && mrLX == 5 * 16)){
            gameCanvas.key6 = false;
            gameCanvas.press6 = false;
            _return = true;
        }

        if(mrLY == 17 * 16 + 8 && ((mrLX < 2 * 16) || (mrLX >= 4 * 16 - 8 && mrLX < 11 * 16) || (mrLX >= 13 * 16 - 8))){
            gameCanvas.key2 = false;
            gameCanvas.press2 = false;
            _return = true;
        }
        //Colides with cay canh
        for(int index = 0; index < 3; index ++){
            cay = (Sprite)listCay.elementAt(index);
            if(mrLY == cay.getY() + 41 - 24 && mrLX > cay.getX() - 16 && mrLX  < cay.getX() + 16){
                gameCanvas.key2 = false;
                gameCanvas.press2 = false;
                _return = true;
            }
            if(mrLX == cay.getX() - 16 && mrLY < cay.getY() + 41 - 24 && mrLY > cay.getY()){
                gameCanvas.key6 = false;
                gameCanvas.press6 = false;
                _return = true;
            }
            if(mrLX == cay.getX() + 16 && mrLY < cay.getY() + 41 - 24 && mrLY > cay.getY()){
                gameCanvas.key4 = false;
                gameCanvas.press4 = false;
                _return = true;
            }
        }
        // update layer wall
        if(mrLY >= 17 * 16 +  8){
            gameCanvas.getLayerManager().remove(wall);
            gameCanvas.getLayerManager().insert(namIcon, 1); // index co the thay doi
            gameCanvas.getLayerManager().insert(nuIcon, 2);
            cay = (Sprite)listCay.elementAt(0);
            gameCanvas.getLayerManager().insert(cay, 3);
        }
        else{
            gameCanvas.getLayerManager().insert(namIcon, 0);
            gameCanvas.getLayerManager().insert(nuIcon, 1); //index co the thay doi
            cay = (Sprite)listCay.elementAt(0);
            gameCanvas.getLayerManager().insert(cay, 2);
            gameCanvas.getLayerManager().insert(wall, 3);
        }
        
        return _return;
    }
    public void npcAction(){
        for(int index = 2; index < 6; index ++){
            npc = NPC.elementAt(index);
            npc.nextFrame();
        }
    }
    public void drawMap(SunnetGameCanvas gameCanvas, Graphics g){
        mrLY = gameCanvas.getMrL().getY();
        mrLX = gameCanvas.getMrL().getX();
        if (mrLY + height / 2 < 25 * 16 && mrLY - height/2 > 0) {
            viewPortY = mrLY- height / 2;
            savePortY = viewPortY;           
        }       
        if(mrLX - width / 2 > 0 && (15 * 16 - mrLX > width / 2)){
            viewPortX = mrLX - width / 2;
            savePortX = viewPortX;    
        }
        gameCanvas.getLayerManager().setViewWindow(viewPortX, viewPortY, width, height);
        gameCanvas.getLayerManager().paint(g, 0, 0);
        npcAction();
       
    }
}
